#include "login.h"
#include "ui_login.h"
#include "regist.h"
#include <QPalette>

Login::Login(int user, QWidget *parent) :
    QWidget(parent),user(user),
    ui(new Ui::Login)
{
    ui->setupUi(this);

    this->setAttribute(Qt::WA_StyledBackground, true);
    this->setObjectName("widget");
    this->setStyleSheet("#widget{background-image: url(:/res/beijing.jpg);}");



    if(user == 0)
    {
        //ui->name_label->setText("名字：");
        //ui->regist_pushButton->setEnabled(false);
    }

    ui->name_lineEdit->installEventFilter(this);
    ui->name_lineEdit->setFocusPolicy(Qt::ClickFocus);
    ui->name_lineEdit->setEchoMode(QLineEdit::Password);

    ui->passwd_lineEdit->installEventFilter(this);
    ui->passwd_lineEdit->setFocusPolicy(Qt::ClickFocus);
    ui->passwd_lineEdit->setEchoMode(QLineEdit::Password);

    ui->login_pushButton->setFocusPolicy(Qt::NoFocus);
    ui->regist_pushButton->setFocusPolicy(Qt::NoFocus);

    key = new KeyBoard(this);
    key->setGeometry(10, 290, 780, 200);
    key->hide();
    key->init_number();
    connect(key, &KeyBoard::itemClick, this, &Login::keyPress);

#ifdef SUPPORT_TEST
    ui->name_lineEdit->setText("admin");
    ui->passwd_lineEdit->setText("admin");
#endif
    update();
}

Login::~Login()
{
    delete ui;
}

void Login::on_login_pushButton_clicked()
{
    QString id = ui->name_lineEdit->text();
    QString passwd = ui->passwd_lineEdit->text();
    if(!id.isEmpty() && !passwd.isEmpty())
    {/*
        if(user == 1)
        {
            User_t user = Sqlite::Get()->Select_user(id, passwd);
            if(user.name.isEmpty())
            {
                QMessageBox::about(this, "error", "id或密码错误！");
            }
            else
            {
                User *u = new User(user);
                u->show();
            }
        }
        else*/
        {
            if(id == "admin" && passwd == "admin")
            {
                Admin *a = new Admin;
                a->show();
                this->close();
                //AdminDialog *a = new AdminDialog(this);
                //a->show();
            }
            else
                QMessageBox::about(this, "error", "密码错误！");
        }
    }
    else
    {
        QMessageBox::about(this, "error", "输入不能为空！！");
    }
}

void Login::on_regist_pushButton_clicked()
{
    this->close();
}

void Login::on_pushButton_clicked()
{
    this->close();
}

void Login::keyPress(QString ch)
{
    qDebug() << ch;
    if(ch == "A/a")
    {
        key->init_larget();
    }
    else if(ch == "a/A")
    {
        key->init_small();
    }
    else if(ch == "123L" || ch == "123l" || ch == "123")
    {
        key->init_number();
    }
    else if(ch == "&")
    {
        if(nameFocuse)
            ui->name_lineEdit->insert("&");
        else
            ui->passwd_lineEdit->insert("&");
    }
    else if(ch == "->")
    {
        deleteBtn_click();
    }
    else
    {
        if(nameFocuse)
            ui->name_lineEdit->insert(ch);
        else
            ui->passwd_lineEdit->insert(ch);
    }
}

bool Login::eventFilter(QObject * watched, QEvent * event)
{

    if (ui->name_lineEdit == watched)
    {
        if (event->type() == QEvent::MouseButtonPress)
        {
            qDebug() << "MouseButtonPress name_lineEdit";
            nameFocuse = true;
            key->show();
        }
    }
    else if(ui->passwd_lineEdit == watched)
    {
        if (event->type() == QEvent::MouseButtonPress)
        {
            qDebug() << "MouseButtonPress passwd_lineEdit";
            nameFocuse = false;
            key->show();
        }
    }

    return QWidget::eventFilter(watched, event);
}

void Login::mouseReleaseEvent(QMouseEvent *)
{
    qDebug() << "mouseReleaseEvent";
    key->hide();
}

void Login::deleteBtn_click()
{
    if(nameFocuse)
    {
        QString text = ui->name_lineEdit->text();
        if(text.isEmpty())
        {
            ui->name_lineEdit->setFocus();
            return ;
        }
        int pos = ui->name_lineEdit->cursorPosition();
        if(pos <= text.size() && pos != 0)
        {
            text.remove(pos-1, 1);
            ui->name_lineEdit->setText(text);
            ui->name_lineEdit->setCursorPosition(pos-1);
        }
    }
    else
    {
        QString text = ui->passwd_lineEdit->text();
        if(text.isEmpty())
        {
            ui->passwd_lineEdit->setFocus();
            return ;
        }
        int pos = ui->passwd_lineEdit->cursorPosition();
        if(pos <= text.size() && pos != 0)
        {
            text.remove(pos-1, 1);
            ui->passwd_lineEdit->setText(text);
            ui->passwd_lineEdit->setCursorPosition(pos-1);
        }
    }
}
